package com.hnu.tockmarket.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;

import com.hnu.tockmarket.R;

/**
 * 启动闪页对应的活动
 *
 * @author hnulht
 * @since 2021-07-07
 */
public class SplashActivity extends AppCompatActivity {

    private static final String TAG = "SplashActivity"; // 日志
    private LinearLayout linearLayout; // 线性布局控件

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState); // 调用父类onCreate()方法
        setContentView(R.layout.activity_splash); // 给启动闪页活动加载对应的布局
        linearLayout = findViewById(R.id.main_ll); // 定位线性布局控件
        setAlphaAnimation(); // 给系统LOGO设置渐变效果
    }

    /**
     * 设置启动图片渐变效果
     */
    private void setAlphaAnimation() {
        AlphaAnimation animation = new AlphaAnimation(0.3f, 1.0f); // 生成动画对象
        animation.setDuration(3000); // 设置持续时间为3s
        linearLayout.setAnimation(animation); // 给线性布局控件设置动画
        // 设置动画监听
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                jumpActivity();
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });
    }

    /**
     * 根据用户是否登录APP跳转到相应界面
     */
    private void jumpActivity() {
        // 获取用户是否登录APP的记录
        SharedPreferences sharedPreferences = getSharedPreferences("login_data", MODE_PRIVATE);
        String id = sharedPreferences.getString("id", "-1");
        Intent intent = null;
        if (id.equals("-1")) {
            intent = new Intent(SplashActivity.this, GuideActivity.class);
        } else {
            intent = new Intent(SplashActivity.this, MainActivity.class);
        }
        startActivity(intent);
        finish();
    }

}